package com.wang.rabbitmqstudy.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.util.StringUtils;

/**
 * RabbitMQ配置
 */
@Configuration
public class RabbitMqConfig {

    private static Logger logger = LoggerFactory.getLogger(RabbitMqConfig.class);

    /**
     * MQ消息模板
     *
     * @param rabbitTemplate 消息模板
     * @return MQ消息模板
     */
    @Bean
    public RabbitMessagingTemplate rabbitMessagingTemplate(RabbitTemplate rabbitTemplate) {
        RabbitMessagingTemplate rabbitMessagingTemplate = new RabbitMessagingTemplate();
        // 发送确认
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            System.out.println("confirmCallback：" + correlationData + "=" + ack + "=" + cause);
        });
        rabbitMessagingTemplate.setMessageConverter(new MappingJackson2MessageConverter());
        rabbitMessagingTemplate.setRabbitTemplate(rabbitTemplate);
        return rabbitMessagingTemplate;

    }
}
